/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package runebound.ui.commons;

import java.util.ArrayList;
import java.util.List;
import runebound.random.RBAzar;
import runebound.random.Tirada;

/**
 *
 * @author Miguel (alakat@gmail.com)
 */
public class RBDadosDiezPanel extends javax.swing.JPanel {

    
    private Tirada tirada;
    private boolean repetirTirada;
    private List<RBTiradaDadosDiezListener> listeners;
    /**
     * Creates new form RBDadosDiezPanel
     */
    public RBDadosDiezPanel() {
        initComponents();
        this.repetirTirada=false;
        this.listeners = new ArrayList<>();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tirarButton = new javax.swing.JButton();
        d1Text = new javax.swing.JLabel();
        d2Text = new javax.swing.JLabel();

        tirarButton.setText("Tira!");
        tirarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tirarButtonActionPerformed(evt);
            }
        });

        d1Text.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        d1Text.setText("0");

        d2Text.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        d2Text.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tirarButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(d1Text, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(d2Text, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(d1Text)
                            .addComponent(d2Text)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tirarButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tirarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tirarButtonActionPerformed
        // TODO add your handling code here:
        this.tirada = RBAzar.getInstance().tiradaCombate();
        /*
         %10 para que el 10 sea un 0 :)
        */
        this.d1Text.setText(""+tirada.getD1()%10);
        this.d2Text.setText(""+tirada.getD2()%10);
        this.tirarButton.setEnabled(false);
        for (RBTiradaDadosDiezListener listener : listeners) {
            listener.tiraradaRealizada(tirada);
        }
    }//GEN-LAST:event_tirarButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel d1Text;
    private javax.swing.JLabel d2Text;
    private javax.swing.JButton tirarButton;
    // End of variables declaration//GEN-END:variables


    public boolean isTirada(){
        return this.tirada!=null;
    }

    public Tirada getTirada() {
        return tirada;
    }
    
    public void setRepetirTirada(){
        if(!this.repetirTirada){
            this.repetirTirada=true;
            this.tirarButton.setEnabled(true);
            for (RBTiradaDadosDiezListener listener : listeners) {
                listener.repetirTiradaAsignada();
            }
        }
    }

    public boolean isRepetirTirada() {
        return repetirTirada;
        
    }
    
    public void addListener(RBTiradaDadosDiezListener listener){
        this.listeners.add(listener);
    }
    
    public void removeListener(RBTiradaDadosDiezListener listener){
        this.listeners.remove(listener);
    }
}
